//
//  HeadDetailsScrollView.m
//  YouWen
//
//  Created by liqiang－pc on 15/3/9.
//  Copyright (c) 2015年 刘永和. All rights reserved.
//

#import "HeadDetailsScrollView.h"

@implementation HeadDetailsScrollView

-(id)initWithFrame:(CGRect)frame headLine:(HeadLine *)headLine
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [NightModeHandle shareNightModeHandle].allViewColor;
//    //title
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 330, 60)];
    self.titleLabel.backgroundColor = [NightModeHandle shareNightModeHandle].allViewColor;
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.textColor = [UIColor blackColor];
    self.titleLabel.font = [UIFont systemFontOfSize:[WordSizeHandle shareWordSizeHandle].detaiTitleSize];
    self.titleLabel.font = [UIFont boldSystemFontOfSize:22]; //加粗方法
   self.titleLabel.text = headLine.title;
    [self addSubview:self.titleLabel];

    //日期
    self.dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 80, 40, 20)];
    self.dateLabel.backgroundColor = [NightModeHandle shareNightModeHandle].allViewColor;
    self.dateLabel.font = [UIFont systemFontOfSize:[WordSizeHandle shareWordSizeHandle].detaiOtherSize];
    self.dateLabel.text = @"日期:";
    [self addSubview:self.dateLabel];
    
    //时间
    
    self.timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 80, 80, 20)];
    self.timeLabel.backgroundColor = [NightModeHandle shareNightModeHandle].allViewColor;
    self.timeLabel.font = [UIFont systemFontOfSize:[WordSizeHandle shareWordSizeHandle].detaiOtherSize];
        self.timeLabel.text = headLine.date;

    [self addSubview:self.timeLabel];
    
    //来自
    self.fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(160, 80, 40, 20)];
    self.fromLabel.backgroundColor = [NightModeHandle shareNightModeHandle].allViewColor;
    self.fromLabel.text = @"来自:";
    self.fromLabel.font = [UIFont systemFontOfSize:[WordSizeHandle shareWordSizeHandle].detaiOtherSize];
    [self addSubview:self.fromLabel];
    
    //地方
    self.addressLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 80, 80, 20)];
    self.addressLabel.backgroundColor = [NightModeHandle shareNightModeHandle].allViewColor;
    self.addressLabel.font = [UIFont systemFontOfSize:[WordSizeHandle shareWordSizeHandle].detaiOtherSize];
        self.addressLabel.text = headLine.laizhi;

    [self addSubview:self.addressLabel];
    
    
    //详情图片
    self.imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(20,110,330,170)];
        self.imageView1.contentMode = UIViewContentModeScaleAspectFit;
        NSString *urlStr = headLine.ImageUrl;
        NSString *urlStrEncode = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url = [NSURL URLWithString:urlStrEncode];
        
        [self.imageView1 sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"picholder@2x.png"]];
        self.imageView1.backgroundColor = [NightModeHandle shareNightModeHandle].allViewColor;
        [self addSubview:self.imageView1];
        

    
    //内容
    
    self.contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 310, 340, 300)];
    self.contentLabel.numberOfLines = 0;
    
    self.contentLabel.font = [UIFont systemFontOfSize:[WordSizeHandle shareWordSizeHandle].detaiTextSize];
    
    self.contentLabel.backgroundColor = [NightModeHandle shareNightModeHandle].allViewColor;
    self.contentLabel.text = headLine.contents;
        
        
        
    [self addSubview:self.contentLabel];
    
    
        // lable自适应高度
        CGRect frame = self.contentLabel.frame;
        frame.size.height = [[self class] heightForContentText:headLine.contents];
        self.contentLabel.frame = frame;
        
        [self addSubview:self.contentLabel];
        
        
        // 将ScrollView的contensize高度超过屏幕的高度, 使整个能纵向滑动
        // contentLable的高度决定整个contensize的高度
        self.contentSize = CGSizeMake(self.frame.size.width, [[self class] heightForContentText:headLine.contents] + 380 );
        
        
        

}
    return self;
}

//// 活动简介lable自适应高度
+ (CGFloat)heightForContentText:(NSString *)text
{
    CGSize size = CGSizeMake(340, 3000);
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:[WordSizeHandle shareWordSizeHandle].detaiTextSize] forKey:NSFontAttributeName];
    CGRect frame = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return frame.size.height;
    
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
